<template>
	<sw-common-frame :loading="loading">
		<template slot="button">
			<el-button class="btn" size="small" type="primary" @click="handleAllForceAmend('2')">全部关闭强制</el-button>
			<el-button class="btn" size="small" type="primary" @click="handleAllForceAmend('1')">全部开启强制</el-button>
		</template>
		<el-form slot="query" size="small" inline>
			<el-form-item>
				<el-input v-model="queryData.account" clearable placeholder="用户账号" style="width: 180px"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" @click="queryTableData()">查询</el-button>
			</el-form-item>
		</el-form>
		<template slot="table">
			<el-table size="medium" :data="tableData" stripe height="100%">
				<el-table-column label="账号和姓名">
					<template slot-scope="{ row }">{{ row.account }}({{ row.userName }})</template>
				</el-table-column>
				<el-table-column prop="deptName" label="所属部门"></el-table-column>
				<el-table-column label="是否强制">
					<template slot-scope="{ row }">
						<el-tag size="medium" v-if="row.isForce === '1'" type="success">是</el-tag>
						<el-tag size="medium" v-if="row.isForce === '2'" type="danger">否</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="90">
					<template slot-scope="{ row }">
						<el-button size="medium" type="text" @click="handleForceOperate(row)">{{ row.isForce === '1' ? '关闭强制' : '开启强制' }}</el-button>
					</template>
				</el-table-column>
			</el-table>
		</template>
		<template slot="page">
			<sw-pagination :total="dataCount" :page.sync="queryData.page" :limit.sync="queryData.limit" @change="queryTableData()"></sw-pagination>
		</template>
	</sw-common-frame>
</template>

<script>
import listMixins from '@/mixins/listMixins';
import { getUserList, forceUpdateAllUserPassword, forceUpdateUserPassword } from '@/api/system/tbSysUser';
export default {
	mixins: [listMixins],
	data() {
		return {};
	},
	computed: {
		listFunction() {
			return eval(getUserList);
		}
	},
	methods: {
		//开启/关闭强制修改
		async handleForceOperate(row) {
			let isForce = row.isForce === '1';
			let params = { id: row.id, isForce: isForce ? '2' : '1' };
			let result = await forceUpdateUserPassword(params).then((res) => res.data);
			if (result.code === '0') {
				this.$message.success(`${isForce ? '关闭' : '开启'}强制修改成功`);
				row.isForce = isForce ? '2' : '1';
			} else {
				this.$message.error(`${isForce ? '关闭' : '开启'}强制修改失败`);
			}
		},

		//全部开启/关闭强制修改
		async handleAllForceAmend(value) {
			let isForce = value === '1';
			let result = await forceUpdateAllUserPassword(value).then((res) => res.data);
			if (result.code === '0') {
				this.$message.success(`全部用户${isForce ? '关闭' : '开启'}强制修改成功`);
				this.queryTableData();
			} else {
				this.$message.error(`全部用户${isForce ? '关闭' : '开启'}强制修改失败`);
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.force_amend_wrapper::v-deep {
	display: flex;
	flex-direction: column;
	flex: 1;
	padding: 16px;
	overflow: hidden;

	.sw_button_box {
		margin-bottom: 16px;

		.el-button.btn {
			float: right;
			margin-left: 10px;
		}

		.el-input {
			width: 240px;
		}

		.el-input-group__append {
			padding: 0 12px;
		}
	}
}
</style>
